package com.hsxshop.controller;

import com.hsxshop.model.GoodsAttribute;
import com.hsxshop.model.Pagination;
import com.hsxshop.service.GoodsAttributeService;
import com.hsxshop.util.*;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangchongya on 17/7/11.
 */
@Controller
@RequestMapping(value = "/attribute")
public class GoodsAttributeController {

    @Resource(name = "goodsAttributeService")
    private GoodsAttributeService goodsAttributeService;

    @RequestMapping(value = "list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String index(@RequestBody Map requestMap) {
        Pagination pagination = PaginationTool.getPagination(requestMap);
        List<GoodsAttribute> lists = goodsAttributeService.findByHQLWithPagination(pagination, SqlTool.returnSql("new GoodsAttribute(id,name,storage,sort,state)", "GoodsAttribute", "", "order by sort desc"), null);

        return JsonTool.returnListJsonSuccess(lists, pagination);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String add(@RequestBody GoodsAttribute goodsAttribute) {

        if ("" == goodsAttribute.getName().trim()) {
            return JsonTool.returnObjectJsonErrorMsg("名称不能为空");
        }

        if (goodsAttributeService.exitsByHQL(SqlTool.returnSql("new GoodsAttribute(id,name,storage,sort,state)", "GoodsAttribute", " where name=?0 and storage=?1", ""), goodsAttribute.getName(), goodsAttribute.getStorage())) {
            return JsonTool.returnObjectJsonErrorMsg("此分类下属性名称已存在");
        }

        if (goodsAttributeService.saveEntity(goodsAttribute) > 0) {
            return JsonTool.returnObjectJsonSuccess(null);
        }
        return JsonTool.returnObjectJsonError(null);
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String detail(@RequestBody Map requestMap) {

        Integer id = (Integer) requestMap.get("id");

        GoodsAttribute attribute = goodsAttributeService.getEntity(id);

        return JsonTool.returnObjectJsonSuccess(attribute);
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String edit(@RequestBody GoodsAttribute goodsAttribute) {
        String sql = "update GoodsAttribute set name=?0,storage=?1,sort=?2,state=?3 where id=?4";
        if (goodsAttributeService.execHQL(sql, goodsAttribute.getName(), goodsAttribute.getStorage(), goodsAttribute.getSort(), goodsAttribute.getState(), goodsAttribute.getId()) == 1) {
            return JsonTool.returnObjectJsonSuccess(null);
        } else {
            return JsonTool.returnObjectJsonError(null);
        }
    }

}
